package com.example.im.ui.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.im.R
import com.example.im.entity.FriendOrRoom
import com.xlkj.ehobe.storage.ui.adapter.holder.RecyclerViewHolder
import kotlinx.android.synthetic.main.recycler_item_room_user.view.*
import org.jivesoftware.smackx.muc.Affiliate


class RoomUserAdapter(
    private val mContext: Context,
    private val owners: MutableList<FriendOrRoom>
) :
    RecyclerView.Adapter<RecyclerViewHolder>() {


    private var onItemClickListener: ((View, Int) -> Unit)? = null

    fun setOnItemClickListener(onItemClickListener: ((View, Int) -> Unit)) {
        this.onItemClickListener = onItemClickListener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder {
        return RecyclerViewHolder(
            LayoutInflater.from(mContext).inflate(
                R.layout.recycler_item_room_user, parent, false
            )
        )
    }

    override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {

        val view: View = holder.itemView
        if (position == owners.size) {
            view.img_user_heard.setImageResource(R.drawable.ic_user_invite)
            view.tv_user_name.text = "邀请"
            view.setOnClickListener { v ->
                onItemClickListener!!.invoke(v, position)
            }
        } else {
            view.img_user_heard.setImageResource(R.drawable.ic_touxiang)
            view.tv_user_name.text = owners[position].nickname
        }
        view.tag = position
    }

    override fun getItemCount(): Int {
        return owners.size + 1
    }

    fun addData(owners: MutableList<FriendOrRoom>) {
        this.owners.clear()
        this.owners.addAll(owners)
        this.notifyDataSetChanged()
    }

    interface OnClickItemListener : (View, Int) -> Unit {
        override fun invoke(p1: View, p2: Int)
    }
}
